package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.widget.HSSegmentControl.OnSegmentControlClickListener;
import com.hundsun.yr.universal.library.widget.NoScrollViewPager;
import com.hundsun.yr.universal.p2p.presenter.base.Presenter;
import com.hundsun.yr.universal.p2p.presenter.impl.MineLoanPresenterImpl;
import com.hundsun.yr.universal.p2p.presenter.impl.MineLoantSegmentInteractorImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseFragment;
import com.hundsun.yr.universal.p2p.ui.adapter.SegmentBasePagerAdapter;
import com.hundsun.yr.universal.p2p.ui.fragment.ApplyingFragment;
import com.hundsun.yr.universal.p2p.view.base.BaseSegmentView;

import java.util.List;


/**
 * @version V1.0
 * @Title: MineLoanActivity.java
 * @Description: 我的借款
 * @date 2016-1-5 下午7:46:38
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class MineLoanActivity extends BaseActivity implements BaseSegmentView,
        OnSegmentControlClickListener ,ViewPager.OnPageChangeListener{
    private Presenter mPresenter = null;
    private NoScrollViewPager noScrollViewPager = null;
    /**
     * fragment适配器
     **/
    private SegmentBasePagerAdapter adapter = null;
    /**
     * tablayout_viewpager的导航栏
     **/
    private TabLayout tabLayout = null;
    private String[] segments;
    private int mPosition;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    protected int getContentViewLayoutID() {
        // TODO Auto-generated method stub
        return R.layout.act_mine_loan;
    }

    @Override
    protected void onEveComming(@SuppressWarnings("rawtypes") EventCenter eventCenter) {
        // TODO Auto-generated method stub

    }

    @Override
    protected boolean isBindEventBusHere() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        // TODO Auto-generated method stub

        setToolbarTitle(R.string.mine_load);

        noScrollViewPager = (NoScrollViewPager) findViewById(R.id.viewpager);
        noScrollViewPager.setNoScroll(false);
        /**标题栏底部横线**/
//        findView(R.id.line_bottom).setVisibility(View.GONE);

        /**tablayout初始化**/
        tabLayout = findView(R.id.tab_layout);
        segments = getResources().getStringArray(R.array.mine_loan2_segment);

        mPresenter = new MineLoanPresenterImpl(this, this, new MineLoantSegmentInteractorImpl());
        mPresenter.initialized();
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    protected void onNetworkConnected(NetType type) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onNetworkDisConnected() {
        // TODO Auto-generated method stub

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 绑定适配器
     *
     * @param viewPager
     * @param fragments
     */
    private void setupViewPager(ViewPager viewPager, List<BaseFragment> fragments) {
        //viewpager和tablayout绑定
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        adapter = new SegmentBasePagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setText(segments[0]);
        tabLayout.getTabAt(1).setText(segments[1]);
        tabLayout.getTabAt(2).setText(segments[2]);
//        tabLayout.getTabAt(3).setText(segments[3]);
    }


    @Override
    public void initializeViews(List<BaseFragment> fragments) {
        // TODO Auto-generated method stub
        if (noScrollViewPager != null) {
            noScrollViewPager.setOffscreenPageLimit(fragments.size());
            setupViewPager(noScrollViewPager, fragments);
        }

        noScrollViewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onSegmentControlClick(int index) {
        noScrollViewPager.setCurrentItem(index, false);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Fragment fragment = adapter.getFragment(mPosition);
            if (fragment instanceof ApplyingFragment){
                if (((ApplyingFragment) fragment).onKeyDown(keyCode,event)){
                    return true;
                }
            }
            return super.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mPosition = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
